package com.example.mycardgame

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    // 玩家信息
    private var playerName = "默认玩家"
    private var playerGender = "男"
    private var playerPhone = "13800138000"
    private var highScore = 0

    // UI控件
    private lateinit var tvName: TextView
    private lateinit var tvGender: TextView
    private lateinit var tvPhone: TextView
    private lateinit var tvHighScore: TextView
    private lateinit var btnModify: Button
    private lateinit var btnStartGame: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        initViews()
        updatePlayerInfo()
        setupButtonListeners()

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private fun initViews() {
        tvName = findViewById(R.id.tv_name)
        tvGender = findViewById(R.id.tv_gender)
        tvPhone = findViewById(R.id.tv_phone)
        tvHighScore = findViewById(R.id.tv_high_score)
        btnModify = findViewById(R.id.btn_modify_info)
        btnStartGame = findViewById(R.id.btn_start_game)
    }

    private fun updatePlayerInfo() {
        tvName.text = "姓名：$playerName"
        tvGender.text = "性别：$playerGender"
        tvPhone.text = "电话：$playerPhone"
        tvHighScore.text = "最高分：$highScore"
    }

    private fun setupButtonListeners() {
        btnModify.setOnClickListener {
            val intent = Intent(this, ModifyInfoActivity::class.java).apply {
                putExtra("name", playerName)
                putExtra("gender", playerGender)
                putExtra("phone", playerPhone)
            }
            startActivityForResult(intent, MODIFY_REQUEST_CODE)
        }

        btnStartGame.setOnClickListener {
            startActivity(Intent(this, CardGameActivity::class.java))
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == MODIFY_REQUEST_CODE && resultCode == RESULT_OK) {
            data?.run {
                playerName = getStringExtra("newName") ?: playerName
                playerGender = getStringExtra("newGender") ?: playerGender
                playerPhone = getStringExtra("newPhone") ?: playerPhone
                updatePlayerInfo()
            }
        }
    }

    companion object {
        const val MODIFY_REQUEST_CODE = 100
    }
}